package com.ewanghuiju.app.ui.home.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.home.OtherContract;
import com.ewanghuiju.app.model.bean.response.PintuanClassifyResponBean;
import com.ewanghuiju.app.presenter.home.OtherPresenter;
import com.ewanghuiju.app.test.bean.MyPageGridBean;
import com.ewanghuiju.app.test.bean.TestBean;
import com.ewanghuiju.app.ui.home.adapter.SearchResultShopListAdapter;
import com.ewanghuiju.app.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wihaohao.PageGridView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 首页中除精选的其他页面
 * @time 2020/2/17
 */
public class OtherFragment extends BaseFragment<OtherPresenter> implements OtherContract.View {
    @BindView(R.id.pageView)
    PageGridView pageView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_zh)
    LinearLayout layoutZh;
    @BindView(R.id.tv_screen_jg)
    TextView tvScreenJg;
    @BindView(R.id.iv_screen_jg_up)
    ImageView ivScreenJgUp;
    @BindView(R.id.iv_screen_jg_down)
    ImageView ivScreenJgDown;
    @BindView(R.id.layout_jg)
    LinearLayout layoutJg;
    @BindView(R.id.tv_screen_yj)
    TextView tvScreenYj;
    @BindView(R.id.iv_screen_yj_up)
    ImageView ivScreenYjUp;
    @BindView(R.id.iv_screen_yj_down)
    ImageView ivScreenYjDown;
    @BindView(R.id.layout_yj)
    LinearLayout layoutYj;
    @BindView(R.id.tv_screen_xl)
    TextView tvScreenXl;
    @BindView(R.id.iv_screen_xl_up)
    ImageView ivScreenXlUp;
    @BindView(R.id.iv_screen_xl_down)
    ImageView ivScreenXlDown;
    @BindView(R.id.layout_xl)
    LinearLayout layoutXl;
    @BindView(R.id.tv_screen_rs)
    TextView tvScreenRs;
    @BindView(R.id.iv_screen_rs_up)
    ImageView ivScreenRsUp;
    @BindView(R.id.iv_screen_rs_down)
    ImageView ivScreenRsDown;
    @BindView(R.id.layout_rs)
    LinearLayout layoutRs;
    @BindView(R.id.tv_screen_yhq)
    TextView tvScreenYhq;
    @BindView(R.id.layout_yhq)
    LinearLayout layoutYhq;

    private SearchResultShopListAdapter pintuanShopListAdapter;

    private int mType;


    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_other;
    }

    @Override
    protected void initEventAndData() {
        layoutRs.setVisibility(View.GONE);
        layoutYj.setVisibility(View.VISIBLE);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mType = bundle.getInt(Constants.IT_HOME_TYPE);
        }
        intSmartRefreshLayout();
        if (mType == 2) {
            mPresenter.getPticatesAll();
        }else{
            initPageGrid();
        }
        intRecyleview();
    }

    protected void intSmartRefreshLayout() {
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(false);//是否启用上拉加载功能
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pintuanShopListAdapter.setNewData(TestBean.getResultShopListResponBeanList(15));
                refreshLayout.finishRefresh(1000);
                ToastUtil.show("当前数量" + pintuanShopListAdapter.getData().size());
            }
        });
    }

    /**
     * 网格翻页
     */
    private void initPageGrid() {
        List<MyPageGridBean> classifyList = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            MyPageGridBean bean = new MyPageGridBean();
            bean.setTitle("淘宝");
//            bean.setImg(R.mipmap.icon_taobao);
            classifyList.add(bean);
        }
        pageView.setData(classifyList);
        pageView.setOnItemClickListener(new PageGridView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastUtil.show("" + position);
            }
        });
    }

    private void intRecyleview() {
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        pintuanShopListAdapter = new SearchResultShopListAdapter(R.layout.item_result_shop_list);
        pintuanShopListAdapter.setNewData(TestBean.getResultShopListResponBeanList(15));
        pintuanShopListAdapter.setHasStableIds(true);
        //加载更多 参考 https://github.com/CymChad/BaseRecyclerViewAdapterHelper/wiki/Pull-up-to-loadMore
        pintuanShopListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //测试数据
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pintuanShopListAdapter.addData(TestBean.getResultShopListResponBeanList(5));
//                        ToastUtil.show("当前数量" + pintuanShopListAdapter.getData().size());
                        if (pintuanShopListAdapter.getData().size() > 40) {
                            pintuanShopListAdapter.loadMoreEnd();
                        } else {
                            pintuanShopListAdapter.loadMoreComplete();
                        }
                    }

                }, 2000);
            }
        }, recyclerView);
        recyclerView.setAdapter(pintuanShopListAdapter);

    }

//    @OnClick({R.id.fiv_today_hot, R.id.fiv_gyzq, R.id.fiv_desq})
//    public void doClick(View view) {
//        if (AntiShake.check(view.getId())) {    //判断是否多次点击
//            return;
//        }
//        switch (view.getId()) {
//            case R.id.fiv_today_hot:
//                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_TODAY_HOT).startActivity();
//                slideRightIn();
//                break;
//            case R.id.fiv_gyzq:
//                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_GYZQ).startActivity();
//                slideRightIn();
//                break;
//            case R.id.fiv_desq:
//                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_DESQ).startActivity();
//                slideRightIn();
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showMoreContent(String list) {

    }

    @Override
    public void showPticatesAll(List<PintuanClassifyResponBean> pcrList) {
        if (pcrList != null) {
            List<MyPageGridBean> classifyList = new ArrayList<>();
//            for (int i = 0; i < 15; i++) {
//                MyPageGridBean bean = new MyPageGridBean();
//                bean.setTitle("淘宝");
//                bean.setImg(R.mipmap.icon_taobao);
//                classifyList.add(bean);
//            }
            for (PintuanClassifyResponBean bean : pcrList) {
                MyPageGridBean myPageGridBean = new MyPageGridBean();
                myPageGridBean.setId(bean.getId());
                myPageGridBean.setTitle(bean.getName());
                myPageGridBean.setImg(bean.getImage());
                classifyList.add(myPageGridBean);
            }
            pageView.setData(classifyList);
            pageView.setOnItemClickListener(new PageGridView.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    ToastUtil.show("" + position);
                }
            });
        }
    }
}
